﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.DAL;
using HamcoWcs.Models.Common;
using HamcoWcs.Service.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Quartz;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace HamcoWcs.Workers
{
    /// <summary>
    /// 里程快照服务
    /// </summary>
    public class MilageWipWorker : WorkerBase
    {
        private readonly AutomaticStorageInfo _asrs;
        public MilageWipWorker(AutomaticStorageInfo asrs, WcsDbContext wcsDbContext, IMapper mapper, ILoggerService logger) : base(wcsDbContext, mapper, logger)
        {
            _asrs = asrs;
        }

        public override Task Execute(IJobExecutionContext context)
        {
            try
            {
                var plc = _asrs.ConveyorDic["Pcon01"];
                var miles = _asrs.MotorMileages;

                var content = plc.DbInfo.First(x => x.DbNum == 103).Buffer;

                var srm1db = _asrs.SrmDic["SrmPlc01"].DbInfo;
                var srm2db = _asrs.SrmDic["SrmPlc02"].DbInfo;
                var srm1c1content = srm1db.First(x => x.DbName == "c1_mile").Buffer;
                var srm1c2content = srm1db.First(x => x.DbName == "c2_mile").Buffer;
                var srm2c1content = srm2db.First(x => x.DbName == "c1_mile").Buffer;
                var srm2c2content = srm2db.First(x => x.DbName == "c2_mile").Buffer;

                foreach (var m in miles)
                {
                    if (m.DeviceType == "pcon")
                    {
                        m.Update(content);
                    }
                    else
                    {
                        if (m.DeviceType == "SrmPlc01C1")
                        {
                            m.UpdateSrm(srm1c1content);
                        }
                        if (m.DeviceType == "SrmPlc01C2")
                        {
                            m.UpdateSrm(srm1c2content);
                        }
                        if (m.DeviceType == "SrmPlc02C1")
                        {
                            m.UpdateSrm(srm2c1content);
                        }
                        if (m.DeviceType == "SrmPlc02C2")
                        {
                            m.UpdateSrm(srm2c2content);
                        }
                    }
                }

                var a = miles.OrderBy(x => x.MachineName).ThenBy(x => x.PortName).ToList();


                var fileName = Path.Combine(GlobalValue.MilageLogFolder, $"{DateTime.Now:yyyyMMdd}");


                var t = JsonConvert.SerializeObject(a, new JsonSerializerSettings()
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    Formatting = Formatting.None,
                });
                File.WriteAllText(fileName, t);
            }
            catch (Exception ex)
            {
                _logger.AddSystemErrorLog("MilageWipWorker error:" + ex.Message);
            }

            return Task.CompletedTask;
        }
    }
}
